import services from '../service';
import _ from 'lodash';
import moment from 'moment'
import { routerRedux } from 'dva'
import { Model, ItemUtils } from '@/utils'
import pageConfig from '../config/pageConfig';
const {namespace,listenAddRouter,listenEditRouter,listenDetailRouter} = pageConfig
const initState = {
    id:undefined, // 编辑或查看对应id
    userData:{}, // 用户详情
}

export default Model.extend({
  namespace: namespace,
  state: _.cloneDeep(initState),
  subscriptions: {
    setup({ dispatch, listen }: any) {
      // 新建路由监听
      listen(listenAddRouter, () => {
        dispatch({
          type: 'updateState',
          payload: {
            ..._.cloneDeep(initState),
          }
        })
        dispatch({type:'feathCatergoey'})
      });
      // 编辑路由监听
      listen(listenEditRouter,(e: any) => {
        const id = e.params[0]
        dispatch({
          type: 'updateState',
          payload: {
            ..._.cloneDeep(initState),
            id,
          }
        })
        dispatch({type:'fetchSpuDetail'})
      })
      // 详情路由监听
      listen(listenDetailRouter,(e: any) => {
        const id = e.params[0]
        dispatch({
          type: 'updateState',
          payload: {
            ..._.cloneDeep(initState),
            id,
          }
        })
        dispatch({type:'fetchSpuDetail'})

      })
    }
  },
    effects: {

    * fetchSpuDetail({ payload }: any, { update, call, put, select }: any): any {
        const { id } = yield select((_: any) => _[namespace]);
        // const data = yield call(services.fetchSpuDetail, { spuId:id });
        const data = {}
        if (data) {
            yield update({
            userData: data,
            })
        }
        },
  },

    reducers: {
  }
})
